package cn.iocoder.yudao.module.member.service.company;

import cn.iocoder.yudao.module.member.controller.admin.company.vo.CompanyPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.CompanyRespVO;
import cn.iocoder.yudao.module.member.controller.admin.company.vo.CompanySaveReqVO;
import cn.iocoder.yudao.module.member.controller.admin.job.vo.AppJobRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.module.member.dal.dataobject.job.ApiJobDO;
import cn.iocoder.yudao.module.member.dal.dataobject.jobmajor.JobMajorDO;
import cn.iocoder.yudao.module.member.dal.mysql.company.CompanyMapper;
import cn.iocoder.yudao.module.member.dal.mysql.job.ApiJobMapper;
import cn.iocoder.yudao.module.member.dal.mysql.jobmajor.JobMajorMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.COMPANY_NOT_EXISTS;

/**
 * 企业 Service 实现类
 *
 * @author Alan
 */
@Service
@Validated
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private ApiJobMapper apiJobMapper;
    
    @Resource
    private JobMajorMapper jobMajorMapper;

    @Override
    public Long createCompany(CompanySaveReqVO createReqVO) {
        // 插入
        CompanyDO company = BeanUtils.toBean(createReqVO, CompanyDO.class);
        companyMapper.insert(company);
        // 返回
        return company.getId();
    }

    @Override
    public void updateCompany(CompanySaveReqVO updateReqVO) {
        // 校验存在
        validateCompanyExists(updateReqVO.getId());
        // 更新
        CompanyDO updateObj = BeanUtils.toBean(updateReqVO, CompanyDO.class);
        companyMapper.updateById(updateObj);
    }

    @Override
    public void deleteCompany(Long id) {
        // 校验存在
        validateCompanyExists(id);
        // 删除
        companyMapper.deleteById(id);
    }

    private void validateCompanyExists(Long id) {
        if (companyMapper.selectById(id) == null) {
            throw exception(COMPANY_NOT_EXISTS);
        }
    }

    @Override
    public CompanyDO getCompany(Long id) {
        return companyMapper.selectById(id);
    }

    @Override
    public PageResult<CompanyDO> getCompanyPage(CompanyPageReqVO pageReqVO) {
        return companyMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<CompanyRespVO> getCompanyVoPage(CompanyPageReqVO pageReqVO) {
        Page<CompanyRespVO> page = new Page<CompanyRespVO>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        if (StringUtils.isNotBlank(pageReqVO.getCompanyNature())) {
            String[] split = pageReqVO.getCompanyNature().split(",");
            pageReqVO.setCompanyNatures(Arrays.asList(split)); // 多个值
        }
        if (StringUtils.isNotBlank(pageReqVO.getMinEducations())) {
            String[] split = pageReqVO.getMinEducations().split(",");
            pageReqVO.setMinEducation(Arrays.asList(split)); // 多个值
        }
        if (StringUtils.isNotBlank(pageReqVO.getJobType())) {
            String[] split = pageReqVO.getJobType().split(",");
            pageReqVO.setJobTypes(Arrays.asList(split)); // 多个值
            pageReqVO.setJobType("");
        }

        IPage<CompanyRespVO> user = companyMapper.getCompanyVoPage(page,pageReqVO);
        List<CompanyRespVO> records = user.getRecords();
        for (CompanyRespVO respVO: records) {
            List<ApiJobDO> apiJobDOS = apiJobMapper.selectList(new LambdaQueryWrapper<ApiJobDO>()
                    .eq(ApiJobDO::getCompanyId, respVO.getId()));
            for (ApiJobDO jobDO:apiJobDOS){
                List<JobMajorDO> jobMajorDOS = jobMajorMapper.selectList(new LambdaQueryWrapper<JobMajorDO>()
                        .eq(JobMajorDO::getJobId, jobDO.getId()));
                if (!CollectionUtils.isEmpty(jobMajorDOS)){
                    String collect = jobMajorDOS.stream().map(JobMajorDO::getMajorName).collect(Collectors.joining(","));
                    jobDO.setMajorName(collect);
                }
            }
            respVO.setAppJobs(apiJobDOS);
        }
        return new PageResult<>(user.getRecords(),user.getTotal());
    }

    @Override
    public List<CompanyDO> getCompanyList(CompanyPageReqVO pageReqVO) {
        return companyMapper.selectList();
    }
}